﻿#include "verifycode.h"


QString VerifyCode::lib= "1234567890";
VerifyCode::VerifyCode(QObject *parent)
    : QObject{parent}
{
    flag =true; //true 为可以重新发送
}

QString VerifyCode::GetCode(){
    return code;
}
void VerifyCode::GenerateCode(){
    srand(QTime(0,0,0).secsTo(QTime::currentTime()));
    if(flag==false){
        QDateTime curtime = QDateTime::currentDateTime();
        int secGap = sendTime.secsTo(curtime);
        qDebug()<<"gap "<<secGap;
        //30s 后可以重发
        if(secGap>30){
            code.clear();
            flag = true;
        }
    }
    if(flag){
        for(int i=0;i<4;i++){
            code+= lib[rand()%lib.length()];
        }
        sendTime = QDateTime::currentDateTime();
        //send
        qDebug()<<"Intialize Code:  "<<code;
        flag = false;
    }
}
//TODO 长久化保存 不然退出就没用
bool VerifyCode::check(QString enter){
    if(code!=""&&enter == code){
        QDateTime curtime = QDateTime::currentDateTime();
        int secGap = sendTime.secsTo(curtime);
        if(secGap<=60*5){
            return true;
        }else{
            qDebug()<<"out of valid time , Please reget the code";
            return false;
        }
    }else{
        qDebug()<<"wrong answer";
        return false;
    }
}
VerifyCode::~VerifyCode(){}

bool VerifyCode::GetStatus(){
    return flag;
}
